package com.weixin.paleituo.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.StoreHomePagerAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.AttentionStoreBean;
import com.weixin.paleituo.bean.CarouselBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.StoreGoodBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.http.RxHelper;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LoadingDialogUtils;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.ForbidScrollViewpager;
import com.weixin.paleituo.view.LoadingPager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 店铺首页
 */
public class GoodsStoreActivity extends BaseActivity {

    @BindView(R.id.iv_back_activity_goods_store)
    ImageView mIvBackActivityGoodsStore;
    @BindView(R.id.iv_store_icon_activity_goods_store)
    ImageView mIvStoreIconActivityGoodsStore;
    @BindView(R.id.tv_storename_activity_goods_store)
    TextView mTvStorenameActivityGoodsStore;
    @BindView(R.id.tv_mounnt_number_activity_goods_store)
    TextView mTvMounntNumberActivityGoodsStore;
    @BindView(R.id.iv_atternt_activity_goods_store)
    ImageView mIvAtterntActivityGoodsStore;
    @BindView(R.id.tv_attem_number_activity_goods_store)
    TextView mTvAttemNumberActivityGoodsStore;
    @BindView(R.id.rl_headroot_activity_goods_store)
    RelativeLayout mRlHeadrootActivityGoodsStore;
    @BindView(R.id.iv_idcator_home_activity_goods_store)
    ImageView mIvIdcatorHomeActivityGoodsStore;
    @BindView(R.id.tv_home_activity_goods_store)
    TextView mTvHomeActivityGoodsStore;
    @BindView(R.id.view_line_home_activity_goods_store)
    View mViewLineHomeActivityGoodsStore;
    @BindView(R.id.ll_firstroot_activity_goods_store)
    LinearLayout mLlFirstrootActivityGoodsStore;
    @BindView(R.id.tv_goodsnumber_activity_goods_store)
    TextView mTvGoodsnumberActivityGoodsStore;
    @BindView(R.id.tv_allgoods_activity_goods_store)
    TextView mTvAllgoodsActivityGoodsStore;
    @BindView(R.id.ll_tworoot_activity_goods_store)
    LinearLayout mLlTworootActivityGoodsStore;
    @BindView(R.id.view_allstore_activity_goods_store)
    View mViewAllstoreActivityGoodsStore;
    @BindView(R.id.forbidScroll_viewpager_activity_goods_store)
    ForbidScrollViewpager mForbidScrollViewpagerActivityGoodsStore;
    private LoadingPager mLoadingPager;
    private int mIsattention;
    private int mStoreId;
    private Dialog mLoadingDialog;

    @BindView(R.id.iv_headroot_activity_goods_store)
    ImageView mImageViewHeadRoot;
    @Override
    protected void initView() {
        mLoadingPager = new LoadingPager(this,Contans.STORE_SOLDOUT_TYPE) {
            @Override
            protected void close() {
            finish();
            }
            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(GoodsStoreActivity.this, R.layout.activity_goods_store);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };
      //  setContentView(R.layout.activity_goods_store);
        setContentView(mLoadingPager);
    }

    /**
     * 重新加载
     *
     */
    private void retry() {
        Observable<StatusCode<StoreGoodBean>> storeHomeDetailsObservable = ApiUtils.getApi().getStoreHomeDetails(Contans.defaultToken, BaseApplication.getUserId(), mStoreId);
        HttpUtil.getInstance().toSubscribe(storeHomeDetailsObservable, new ProgressSubscriber<StoreGoodBean>(this) {
            @Override
            protected void _onNext(StatusCode<StoreGoodBean> storeGoodBeanStatusCode) {

                StoreGoodBean storeGoodBean = storeGoodBeanStatusCode.getData();
                List<CarouselBean> carousel = storeGoodBean.getCarousel();


                if (storeGoodBean!=null &&carousel!=null &&carousel.size()>0){

                    mLoadingPager.showPagerView(Contans.STATE_SUCCEED);

                    mIsattention = storeGoodBean.getIsAttention();
                    showAttentIcon(mIsattention);

                    //
                    mTvAttemNumberActivityGoodsStore.setText(storeGoodBean.getAttention()+"人");

                    Glide.with(GoodsStoreActivity.this).load(storeGoodBean.getLogoimg()).into(mIvStoreIconActivityGoodsStore);
                    mTvStorenameActivityGoodsStore.setText(storeGoodBean.getStoreName());
                    mTvMounntNumberActivityGoodsStore.setText("口碑值"+storeGoodBean.getWordOfMouth());

                    mTvGoodsnumberActivityGoodsStore.setText(storeGoodBean.getGoodsnum()+"");

                    StoreHomePagerAdapter storeHomePagerAdapter=new StoreHomePagerAdapter(getSupportFragmentManager(),mStoreId,carousel);

                    mForbidScrollViewpagerActivityGoodsStore.setAdapter(storeHomePagerAdapter);
                    String bgimg = storeGoodBean.getBgimg();

                    if (!StringUtils.isEmpty(bgimg)){
                        UIUtils.loadImageView(GoodsStoreActivity.this,bgimg,mImageViewHeadRoot);
                       /* Glide.with(GoodsStoreActivity.this).load(bgimg).asBitmap().into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {


                                Bitmap bitmap = BlurUtil.doBlur(resource, 10, false);

                                mRlHeadrootActivityGoodsStore.setBackground(new BitmapDrawable(getResources(), bitmap));


                            }
                        });*/

                    }


                }else {

                    mLoadingPager.showPagerView(Contans.STATE_EMPTY);
                }
            }
            @Override
            protected void _onError(String message) {
                if (Contans.STORE_SOLD_OUT.equalsIgnoreCase(message)){
                    mLoadingPager.showPagerView(Contans.STATE_SOLDOUT);
                }else {
                mLoadingPager.showPagerView(Contans.STATE_ERROR);
                }

            }
        },"",lifecycleSubject,false,true);
    }
    /**
     * 关注图片
     * @param isattention
     */
    private  void showAttentIcon(int isattention) {
        if (isattention ==2){
            //关注
            mIvAtterntActivityGoodsStore.setImageResource(R.mipmap.follow_change);
        }else {
            //未关注
            mIvAtterntActivityGoodsStore.setImageResource(R.mipmap.follow);

        }
    }

    @Override
    protected void initData() {
        super.initData();
      //  UIUtils.showFullScreen(this,true);
        Intent intent = getIntent();
        mStoreId = intent.getIntExtra(Contans.INTENT_DATA, 0);
/*        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        mImageViewHeadRoot.setColorFilter(colorFilter);*/
        retry();
    }


    @OnClick(R.id.iv_back_activity_goods_store)
    public void onMIvBackActivityGoodsStoreClicked() {
        finish();
    }

    @OnClick(R.id.iv_atternt_activity_goods_store)
    public void onMIvAtterntActivityGoodsStoreClicked() {

        if (BaseApplication.isLoginSuccess){
           String mark="";

            if (mIsattention==2){
                //未关注
                mark="1";
            }else if (mIsattention==1){
                //已关注
                mark="2";
            }
            Observable observable = ApiUtils.getApi().attentStroe(BaseApplication.getUserId(), mStoreId, mark,BaseApplication.sUserInfoBean.token).compose(RxHelper.getObservaleTransformer()).doOnSubscribe(new Consumer<Disposable>() {
                @Override
                public void accept(Disposable disposable) throws Exception {
                    if (mLoadingDialog == null) {

                        mLoadingDialog = LoadingDialogUtils.createLoadingDialog(GoodsStoreActivity.this, "");
                    }
                    LoadingDialogUtils.show(mLoadingDialog);
                }
            }).subscribeOn(AndroidSchedulers.mainThread());

            HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<AttentionStoreBean>(GoodsStoreActivity.this) {
                @Override
                protected void _onNext(StatusCode<AttentionStoreBean> stringStatusCode) {
                    LoadingDialogUtils.closeDialog(mLoadingDialog);

                    AttentionStoreBean dataBean = stringStatusCode.getData();

                    if (dataBean!=null){
                        String isAttention = dataBean.getIsAttention();
                        if (!StringUtils.isEmpty(isAttention)){
                            mIsattention=Integer.valueOf(isAttention);
                            showAttentIcon(mIsattention);
                        }
                        String followers = dataBean.getFollowers();
                        mTvAttemNumberActivityGoodsStore.setText(followers+"人");

                    }


                }

                @Override
                protected void _onError(String message) {
                    LoadingDialogUtils.closeDialog(mLoadingDialog);
                    ToastUtils.makeText(message);

                }
            },"",lifecycleSubject,false,true);

        }else {
            LoginActivity.startActivity(this,true);
        }




    }

    @OnClick(R.id.ll_firstroot_activity_goods_store)
    public void onMLlFirstrootActivityGoodsStoreClicked() {

        mIvIdcatorHomeActivityGoodsStore.setImageResource(R.mipmap.shop_home);
        mTvHomeActivityGoodsStore.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
        mViewLineHomeActivityGoodsStore.setVisibility(View.VISIBLE);

        mTvGoodsnumberActivityGoodsStore.setTextColor(UIUtils.getColor(R.color.c_383838));
        mTvAllgoodsActivityGoodsStore.setTextColor(UIUtils.getColor(R.color.c_383838));
        mViewAllstoreActivityGoodsStore.setVisibility(View.INVISIBLE);
        mForbidScrollViewpagerActivityGoodsStore.setCurrentItem(0,false);

    }

    @OnClick(R.id.ll_tworoot_activity_goods_store)
    public void onMLlTworootActivityGoodsStoreClicked() {
        mIvIdcatorHomeActivityGoodsStore.setImageResource(R.mipmap.shop_homeb);
        mTvHomeActivityGoodsStore.setTextColor(UIUtils.getColor(R.color.c_383838));
        mViewLineHomeActivityGoodsStore.setVisibility(View.INVISIBLE);
        mTvGoodsnumberActivityGoodsStore.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
        mTvAllgoodsActivityGoodsStore.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
        mViewAllstoreActivityGoodsStore.setVisibility(View.VISIBLE);
        mForbidScrollViewpagerActivityGoodsStore.setCurrentItem(1,false);

    }

    public static void startActivity(Context context,int storeId){

        Intent intent=new Intent(context,GoodsStoreActivity.class);
        intent.putExtra(Contans.INTENT_DATA,storeId);
        context.startActivity(intent);
    }
}
